﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class BaseDAL
    {
        public static DatabaseProperty CONN_TopicSharing = DBSettings.GetDatabaseProperty("ConnectionString.Social");

        public static string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString.Social.Reader"].ConnectionString;  

        public T Get<T>(object id) where T : class
        {
            return CONN_TopicSharing.Get<T>(id);
        }

        public T GetBySql<T>(string sql) where T : class
        {
            return CONN_TopicSharing.Get<T>(sql);
        }

        public bool Insert<T>(T t) where T : class
        {
            return CONN_TopicSharing.Insert<T>(t);
        }

        public bool Update<T>(T t) where T : class
        {
            return CONN_TopicSharing.Update(t);
        }

        public bool Delete<T>(T entity) where T : class
        {
            return CONN_TopicSharing.Delete<T>(entity);
        }

        public int Count(string sql, object param)
        {
            return CONN_TopicSharing.SQLQuery<int>(sql, param).First();
        }

        public IEnumerable<T> List<T>(string sql, object param) where T : class
        {
            return CONN_TopicSharing.SQLQuery<T>(sql, param);
        }

        public IEnumerable<T> ListWhere<T>(string strWhere, object param) where T : class
        {
            return CONN_TopicSharing.SQLQueryWhere<T>(strWhere, param);
        }

        public int Excute(string sql, object param = null)
        {
            return CONN_TopicSharing.Excute(sql, param);
        }

        public IEnumerable<T> ListPage<T>(string sql, out int count, object param = null) where T : class
        {
            return CONN_TopicSharing.SQLPager<T>(sql, out count, param);
        }

        public IEnumerable<T> ListPage<T>(string columns, string table, string strWhere, string grouping, string having, string orderbys, int pagesize, int pageindex, out int count, object param = null) where T : class
        {
            return CONN_TopicSharing.SQLPager<T>(columns, table, strWhere, grouping, having, orderbys, pagesize, pageindex, out count, param);
        }

       

        /// <summary>
        /// 更新采集源
        /// </summary>
        /// <param name="promoter_id">推广者id</param>
        /// <param name="cs_type">采集员类型</param>
        /// <returns></returns>
        public bool UpdateCollectingSourceType(string promoter_id, string cs_type)
        {

            string sql = " update [SysConfig] set collecting_source_type=@cs_type where promoter_id=@promoter_id ";

            return CONN_TopicSharing.Excute(sql, new { promoter_id = promoter_id, cs_type = cs_type }) > 0;

        }
    }
}
